package com.finchina.myview.viewgroups;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.finchina.myview.R;

import java.util.LinkedList;

/**
 * @author 黄赛赛
 * @version V1.0
 * @Title: ${file_name}
 * @Package ${package_name}
 * @Description: ${todo}()
 * @date 2015/10/2 10:35
 */
public class SixModuleLayout extends ViewGroup {

    Paint mPaint;
    float center_radius, module_radius, line_width, circle_arc_divider;
    int circleColor;
    int arc_finish_color;
    int arc_donging_color;
    int centerX, centerY;
    int circleRadius;
    int paddingLeft, paddingRight, paddingTop, paddingBottom,  maxPadding;
    float halfIntersectDegree;//小圆和大圆相交一半角度

    int finishStep = 2;//完成到第几步
    int pauseStep = 3;// 暂停在第几步
    RectF oval = new RectF();


    View pauseView;
    LinkedList<View> finishViewList ;

    public SixModuleLayout(Context context) {
        this(context, null);
    }

    public SixModuleLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SixModuleLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray ta = getContext().getTheme().obtainStyledAttributes(attrs, R.styleable.SixModuleView, defStyleAttr, 0);
        //获取半斤
        center_radius = ta.getDimensionPixelOffset(R.styleable.SixModuleView_center_radius,60);
        module_radius = ta.getDimensionPixelOffset(R.styleable.SixModuleView_module_radius,100);
        line_width = ta.getDimensionPixelOffset(R.styleable.SixModuleView_line_width, 2);
        circle_arc_divider = ta.getDimensionPixelOffset(R.styleable.SixModuleView_circle_arc_divider, 5);
        // 获取颜色
        circleColor = ta.getColor(R.styleable.SixModuleView_circle_color, Color.RED);
        arc_finish_color = ta.getColor(R.styleable.SixModuleView_circle_color, Color.BLUE);
        arc_donging_color = ta.getColor(R.styleable.SixModuleView_circle_color, Color.YELLOW);

        ta.recycle();


        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
        mPaint.setStrokeWidth(line_width);



    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int cCount = getChildCount();
        int cWidth = 0;
        int cHeight = 0;
        for (int i= 0; i <cCount; i++){
            View child = getChildAt(i);
            cWidth = child.getMeasuredWidth();
            cHeight = child.getMeasuredHeight();
            switch (i){
                case 0:
                    child.layout(centerX - cWidth,centerY-cHeight, centerX+cWidth, centerY+cHeight);
                    break;
               default:
                   int degree = -60+ (i-1)*60;
                   int cosX = (int) (circleRadius *Math.cos(Math.toRadians(degree)));
                   int sinY = (int) (circleRadius * Math.sin(Math.toRadians(degree)));
                   int childCenterX = centerX+cosX ;
                   int childCentrY = centerY + sinY;
                   child.layout((int)(childCenterX - module_radius),(int)(childCentrY-module_radius), (int)(childCenterX+module_radius), (int)(childCentrY+module_radius));
                    break;
            }
        }
        layoutProcessingViews();
    }

    void layoutProcessingViews(){
        if (pauseStep >0){
            if (pauseView == null){
                pauseView = LayoutInflater.from(getContext()).inflate(R.layout.sixmodule_finish_ll, null );
                setProcessView(pauseView, R.drawable.ic_insert, "进行中");
                pauseView.measure(MeasureSpec.AT_MOST, MeasureSpec.AT_MOST);
            }
            int degree = -60 + (pauseStep - 1)* 60;
            layoutProcessView(pauseView, degree);
        }

        if (finishStep > 0){
            if (finishViewList == null) {
                finishViewList = new LinkedList<View>();
            }
            if (finishStep > finishViewList.size()){
                for (int i = finishViewList.size(), size = finishStep; i <size; i++){
                  View   v = LayoutInflater.from(getContext()).inflate(R.layout.sixmodule_finish_ll, null );
                    finishViewList.add(v);
                }
            }else {
                for (int i = finishViewList.size(), size = finishStep; i > size; i--){
                    View v = finishViewList.get(i-1);
                    if (this.indexOfChild(v) != -1){
                        this.removeView(v);
                    }
                    finishViewList.removeLast();
                }
            }

            for (int i =0,size = finishStep; i < size; i++){
                View v = finishViewList.get(i);
                int degree = -60 + i* 60;
                layoutProcessView(v, degree);
            }
        }
    }

    /**
     * 布局单个进度的view
     * @param v
     * @param degree
     */
    void layoutProcessView(View v , int degree){

        double radians = Math.toRadians(degree);
        int x = (int) ((circleRadius/2) * Math.cos(radians) + centerX);
        int y = (int) ((circleRadius / 2) * Math.sin(radians) + centerY);
        int w = v.getMeasuredWidth()/2;
        int h = v.getMeasuredHeight()/2;
        v.layout(x -w, y - h, x +w, y+h );
    }

    /**
     * 设置单个进度view 显示的图片和文字
     * @param v
     * @param imgId
     * @param name
     */
    void setProcessView(View v, int imgId, String name){
        ImageView icon = (ImageView) findViewById(R.id.iv_icon);
        TextView tv_name = (TextView) findViewById(R.id.tv_name);
        icon.setImageResource(imgId);
        tv_name.setText(name);
    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        measureChildren(widthMeasureSpec, heightMeasureSpec);
        if (getChildCount()>1){
            View ModuleView = getChildAt(1);
            module_radius = ModuleView.getMeasuredWidth()/2;
        }
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);


        int width =0, height = 0;
        if (widthMode == MeasureSpec.EXACTLY){// match_parent , accurate
            width = widthSize;
        }else {

            width = widthSize;
        }


        if (heightMode == MeasureSpec.EXACTLY){
            height = heightSize;
        }else {
            height = Math.min(width, heightSize);
        }


        setMeasuredDimension(width,height);


        centerX = width/2;
        centerY = height /2;
        paddingLeft = getPaddingLeft();
        paddingRight = getPaddingRight();
        paddingTop = getPaddingTop();
        paddingBottom = getPaddingBottom();

        maxPadding = Math.max(Math.max(paddingLeft, paddingRight), Math.max(paddingTop, paddingBottom));



        circleRadius = (int) (Math.min(width, height)/2 -maxPadding - module_radius);

        halfIntersectDegree =  (float)Math.toDegrees(Math.asin((float)(module_radius + circle_arc_divider)/(float)(2*circleRadius) ));
        halfIntersectDegree = halfIntersectDegree*2;



    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawCircle(canvas);
    }
    void drawCircle(Canvas canvas) {
        canvas.save();

        canvas.translate(centerX, centerY);
        if (finishStep > 0){// 画出已完成的扇型
            mPaint.setColor(arc_finish_color);
            mPaint.setStyle(Paint.Style.FILL);
            oval.set(-circleRadius, -circleRadius, circleRadius, circleRadius);
            canvas.drawArc(oval, -90, finishStep * 60, true, mPaint);
        }
        if (pauseStep > 0){//画出暂停的扇型
            oval.set(-circleRadius, -circleRadius, circleRadius, circleRadius);
            mPaint.setColor(arc_donging_color);
            mPaint.setStyle(Paint.Style.FILL);
            canvas.drawArc(oval, -90 + (pauseStep - 1) * 60, 60, true, mPaint);

        }

        if (finishStep > 0){//画出已完成扇型间间隔线
            mPaint.setColor(Color.WHITE);
            mPaint.setStyle(Paint.Style.FILL);
            for (int i=0;i<finishStep ; i++){
                double angrad = Math.toRadians( - 30+60 * i);
                canvas.drawLine(0, 0, (float) Math.cos(angrad) * circleRadius, (float) Math.sin(angrad) * circleRadius, mPaint);
            }
        }

        if (pauseStep == 6){//画出暂停的扇型间间隔线
            mPaint.setColor(Color.WHITE);
            mPaint.setStyle(Paint.Style.FILL);
            double angrad = Math.toRadians( - 90);
            canvas.drawLine(0, 0, (float) Math.cos(angrad) * circleRadius, (float) Math.sin(angrad) * circleRadius, mPaint);
        }



        mPaint.setColor(circleColor);
        mPaint.setStyle(Paint.Style.STROKE);

//        Log.e("degress","degress =" + halfIntersectDegree);
        oval.set(-circleRadius, -circleRadius, circleRadius, circleRadius);
        for (int i = 0, size = 6; i < size; i++) {
            canvas.drawArc(oval,(float)halfIntersectDegree, 60.0f- (float)(2*halfIntersectDegree),false,mPaint );
            canvas.rotate(60);
        }

        oval.set(circleRadius - module_radius - circle_arc_divider, -module_radius - circle_arc_divider, circleRadius + module_radius + circle_arc_divider, module_radius + circle_arc_divider);


        for (int i = 0, size = 6; i < size; i++) {
            mPaint.setColor(Color.WHITE);
            mPaint.setStyle(Paint.Style.FILL);
            canvas.drawCircle(circleRadius, 0, module_radius + circle_arc_divider,mPaint);
            mPaint.setColor(circleColor);
            mPaint.setStyle(Paint.Style.STROKE);
            canvas.drawArc(oval, 90 + (halfIntersectDegree / 2.0f), 180 - halfIntersectDegree, false, mPaint);

            canvas.rotate(60);
        }
        canvas.restore();
    }

    void refreshStep(int finishStep, int pauseStep){
        this.finishStep = finishStep;
        this.pauseStep = pauseStep;
        invalidate();

    }

}
